package chaojishuzu;

import duixiang.student;

import javax.swing.*;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 主界面
 */
public class StudentUI {
    Scanner sc = new Scanner(System.in);
    SuperArray superArray = new SuperArray();
    /**
     * 菜单
     */
    public void menu() {
        JOptionPane.showMessageDialog(null,"欢迎使用学生管理系统");
        String input;
        while (true) {
            input = JOptionPane.showInputDialog(null,"1-添加学生 2-删除学生 3-修改学生 4-查询学生 5-退出");
            if ("1".equals(input)) {
                    add();
                } else if ("2".equals(input)) {
                    delete();
                } else if ("3".equals(input)) {
                    alter();
                } else if ("4".equals(input)) {
                    search();
                } else if ("5".equals(input)) {
                    JOptionPane.showMessageDialog(null,"再见");
                    System.exit(0);
                }else {
                    JOptionPane.showMessageDialog(null,"输入有误");
                }
            }
        }


    /**
     * 增加
     */
    public void add() {
        JOptionPane.showMessageDialog(null,"这是增加学生信息");
        String pattern = "^[\\u4e00-\\u9fa5]{0,}$";
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
            while (true){
                if (name.matches(pattern)){
                    JOptionPane.showMessageDialog(null,"姓名添加成功");
                    break;
                }else {
                    JOptionPane.showMessageDialog(null,"姓名添加失败，返回主菜单");
                    menu();
                }
            }
                 String pattern1 = "男";
                 String pattern2 = "女";
                 String gender = JOptionPane.showInputDialog(null,"请输入学生性别：");
            while (true){
            if (pattern1.equals(gender) || pattern2.equals(gender)){
                JOptionPane.showMessageDialog(null,"性别添加成功");
                break;
            }else {
                JOptionPane.showMessageDialog(null,"性别添加失败，返回主菜单");
                menu();
            }
        }
        int age = 0;
        while (true){
            try {
                age = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学生年龄："));
            }catch (Exception e){
                JOptionPane.showMessageDialog(null,"年龄必须为数字");
                continue;
            }
            break;
        }
        JOptionPane.showMessageDialog(null,"添加成功请重新选择服务类型");
        Student student = new Student(name,gender,age);
        superArray.add(student);
    }

    /**
     * 删除
     */
    public void delete() {
        JOptionPane.showMessageDialog(null,"这是删除学生信息");
        String name = JOptionPane.showInputDialog(null,"请输入学生姓名：");
        for (int i = 0; i < superArray.size(); i++) {
           // Student student = (Student) superArray.get(i);//强转
            if (superArray.get(i).equals(new Student(name))) {
                superArray.remove(i);
                JOptionPane.showMessageDialog(null,"删除学生信息成功");
                return;
            }
        }
        JOptionPane.showMessageDialog(null,"该学生不存在");
    }

    /**
     * 修改
     */
    public void alter() {
        JOptionPane.showMessageDialog(null,"这是修改学生信息");
        String name = JOptionPane.showInputDialog(null,"请输入要修改的学生姓名：");
        for (int i = 0;i < superArray.size();i++){
            Student student = (Student) superArray.get(i);
            if (student.getName().equals(name)){
                String newName;
                String pattern = "^[\\u4e00-\\u9fa5]{0,}$";
                newName = JOptionPane.showInputDialog(null,"请输入学生姓名：");
                while (true){
                    if (newName.matches(pattern)){
                        JOptionPane.showMessageDialog(null,"新姓名添加成功");
                        break;
                    }else {
                        JOptionPane.showMessageDialog(null,"新姓名添加失败，返回主菜单");
                        menu();
                    }
                }
                student.setName(newName);
                String newGender;
                String pattern1 = "男";
                String pattern2 = "女";
                newGender = JOptionPane.showInputDialog(null,"请输入学生性别：");
                while (true){
                    if (pattern1.equals(newGender) || pattern2.equals(newGender)){
                        JOptionPane.showMessageDialog(null,"性别添加成功");
                        break;
                    }else {
                        JOptionPane.showMessageDialog(null,"性别添加失败，返回主菜单");
                        menu();
                    }
                }
                student.setGender(newGender);
                int newAge = 0;
               while (true){
                   try {
                       newAge = Integer.parseInt(JOptionPane.showInputDialog(null,"请输入学生年龄："));
                       student.setAge(newAge);
                       JOptionPane.showMessageDialog(null,"修改成功，请重新选择服务类型");
                   }catch (InputMismatchException e){
                       JOptionPane.showMessageDialog(null,"年龄必须输入数字");
                       continue;
                   }
                   break;
               }
            }
        }
    }
        /**
         * 查询
         */
        public void search() {
            Boolean index = false;
            String input = JOptionPane.showInputDialog(null,"请选择 1-全部学生 2-学生姓名");
            if (input .equals("1")){
                for (int i = 0; i < superArray.size(); i++){
                    JOptionPane.showMessageDialog(null,superArray.get(i));
                }
            }else if (input.equals("2")){
                String name = JOptionPane.showInputDialog(null,"请输入学生姓名: ");
                Pattern pattern = Pattern.compile(name);
                for (int i = 0; i < superArray.size(); i++) {
                    Student student = (Student) superArray.get(i);
                    Matcher m = pattern.matcher(student.getName());
                    if (m.find()) {
                        JOptionPane.showMessageDialog(null,superArray.get(i));
                        index = true;
                    }
                }
                if (!index){
                    JOptionPane.showMessageDialog(null,"该学生不存在");
                }
            }else {
                JOptionPane.showMessageDialog(null,"输入有误");
            }


        }
}
